{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "home_folder = os.path.expanduser(\"~\")\n",
    "data_folder = os.path.join(home_folder, \"Data\", \"basketball\")\n",
    "data_filename = os.path.join(data_folder, \"leagues_NBA_2014_games_games.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th>NaN</th>\n",
       "      <th>Visitor/Neutral</th>\n",
       "      <th>PTS</th>\n",
       "      <th>Home/Neutral</th>\n",
       "      <th>PTS</th>\n",
       "      <th>NaN</th>\n",
       "      <th>Notes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Tue Oct 29 2013</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">Box Score</th>\n",
       "      <th>Orlando Magic</th>\n",
       "      <th>87</th>\n",
       "      <th>Indiana Pacers</th>\n",
       "      <th>97</th>\n",
       "      <th>NaN</th>\n",
       "      <th>NaN</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Los Angeles Clippers</th>\n",
       "      <th>103</th>\n",
       "      <th>Los Angeles Lakers</th>\n",
       "      <th>116</th>\n",
       "      <th>NaN</th>\n",
       "      <th>NaN</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chicago Bulls</th>\n",
       "      <th>95</th>\n",
       "      <th>Miami Heat</th>\n",
       "      <th>107</th>\n",
       "      <th>NaN</th>\n",
       "      <th>NaN</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Wed Oct 30 2013</th>\n",
       "      <th>Box Score</th>\n",
       "      <th>Brooklyn Nets</th>\n",
       "      <th>94</th>\n",
       "      <th>Cleveland Cavaliers</th>\n",
       "      <th>98</th>\n",
       "      <th>NaN</th>\n",
       "      <th>NaN</th>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: [(Date, nan, Visitor/Neutral, PTS, Home/Neutral, PTS, nan, Notes), (Tue Oct 29 2013, Box Score, Orlando Magic, 87, Indiana Pacers, 97, nan, nan), (Tue Oct 29 2013, Box Score, Los Angeles Clippers, 103, Los Angeles Lakers, 116, nan, nan), (Tue Oct 29 2013, Box Score, Chicago Bulls, 95, Miami Heat, 107, nan, nan), (Wed Oct 30 2013, Box Score, Brooklyn Nets, 94, Cleveland Cavaliers, 98, nan, nan)]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = pd.read_csv(data_filename)\n",
    "results.ix[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1230, 8)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Score Type</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>OT?</th>\n",
       "      <th>Notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Orlando Magic</td>\n",
       "      <td>  87</td>\n",
       "      <td>      Indiana Pacers</td>\n",
       "      <td>  97</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td> Los Angeles Clippers</td>\n",
       "      <td> 103</td>\n",
       "      <td>  Los Angeles Lakers</td>\n",
       "      <td> 116</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Chicago Bulls</td>\n",
       "      <td>  95</td>\n",
       "      <td>          Miami Heat</td>\n",
       "      <td> 107</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Brooklyn Nets</td>\n",
       "      <td>  94</td>\n",
       "      <td> Cleveland Cavaliers</td>\n",
       "      <td>  98</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Atlanta Hawks</td>\n",
       "      <td> 109</td>\n",
       "      <td>    Dallas Mavericks</td>\n",
       "      <td> 118</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>   Washington Wizards</td>\n",
       "      <td> 102</td>\n",
       "      <td>     Detroit Pistons</td>\n",
       "      <td> 113</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date Score Type          Visitor Team  VisitorPts  \\\n",
       "0 2013-10-29  Box Score         Orlando Magic          87   \n",
       "1 2013-10-29  Box Score  Los Angeles Clippers         103   \n",
       "2 2013-10-29  Box Score         Chicago Bulls          95   \n",
       "3 2013-10-30  Box Score         Brooklyn Nets          94   \n",
       "4 2013-10-30  Box Score         Atlanta Hawks         109   \n",
       "5 2013-10-30  Box Score    Washington Wizards         102   \n",
       "\n",
       "             Home Team  HomePts  OT? Notes  \n",
       "0       Indiana Pacers       97  NaN   NaN  \n",
       "1   Los Angeles Lakers      116  NaN   NaN  \n",
       "2           Miami Heat      107  NaN   NaN  \n",
       "3  Cleveland Cavaliers       98  NaN   NaN  \n",
       "4     Dallas Mavericks      118  NaN   NaN  \n",
       "5      Detroit Pistons      113  NaN   NaN  "
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Don't read the first row, as it is blank, and parse the date column as a date\n",
    "results = pd.read_csv(data_filename, parse_dates=[\"Date\"], skiprows=[0,])\n",
    "# Fix the name of the columns\n",
    "results.columns = [\"Date\", \"Score Type\", \"Visitor Team\", \"VisitorPts\", \"Home Team\", \"HomePts\", \"OT?\", \"Notes\"]\n",
    "\n",
    "results.ix[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Score Type</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>OT?</th>\n",
       "      <th>Notes</th>\n",
       "      <th>HomeWin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Orlando Magic</td>\n",
       "      <td>  87</td>\n",
       "      <td>      Indiana Pacers</td>\n",
       "      <td>  97</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td> Los Angeles Clippers</td>\n",
       "      <td> 103</td>\n",
       "      <td>  Los Angeles Lakers</td>\n",
       "      <td> 116</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Chicago Bulls</td>\n",
       "      <td>  95</td>\n",
       "      <td>          Miami Heat</td>\n",
       "      <td> 107</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Brooklyn Nets</td>\n",
       "      <td>  94</td>\n",
       "      <td> Cleveland Cavaliers</td>\n",
       "      <td>  98</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Atlanta Hawks</td>\n",
       "      <td> 109</td>\n",
       "      <td>    Dallas Mavericks</td>\n",
       "      <td> 118</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>   Washington Wizards</td>\n",
       "      <td> 102</td>\n",
       "      <td>     Detroit Pistons</td>\n",
       "      <td> 113</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date Score Type          Visitor Team  VisitorPts  \\\n",
       "0 2013-10-29  Box Score         Orlando Magic          87   \n",
       "1 2013-10-29  Box Score  Los Angeles Clippers         103   \n",
       "2 2013-10-29  Box Score         Chicago Bulls          95   \n",
       "3 2013-10-30  Box Score         Brooklyn Nets          94   \n",
       "4 2013-10-30  Box Score         Atlanta Hawks         109   \n",
       "5 2013-10-30  Box Score    Washington Wizards         102   \n",
       "\n",
       "             Home Team  HomePts  OT? Notes HomeWin  \n",
       "0       Indiana Pacers       97  NaN   NaN    True  \n",
       "1   Los Angeles Lakers      116  NaN   NaN    True  \n",
       "2           Miami Heat      107  NaN   NaN    True  \n",
       "3  Cleveland Cavaliers       98  NaN   NaN    True  \n",
       "4     Dallas Mavericks      118  NaN   NaN    True  \n",
       "5      Detroit Pistons      113  NaN   NaN    True  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results[\"HomeWin\"] = results[\"VisitorPts\"] < results[\"HomePts\"]\n",
    "# Our \"class values\"\n",
    "y_true = results[\"HomeWin\"].values\n",
    "results.ix[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Home Win percentage: 58.0%\n"
     ]
    }
   ],
   "source": [
    "print(\"Home Win percentage: {0:.1f}%\".format(100 * results[\"HomeWin\"].sum() / results[\"HomeWin\"].count()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Score Type</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>OT?</th>\n",
       "      <th>Notes</th>\n",
       "      <th>HomeWin</th>\n",
       "      <th>HomeLastWin</th>\n",
       "      <th>VisitorLastWin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Orlando Magic</td>\n",
       "      <td>  87</td>\n",
       "      <td>      Indiana Pacers</td>\n",
       "      <td>  97</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td> Los Angeles Clippers</td>\n",
       "      <td> 103</td>\n",
       "      <td>  Los Angeles Lakers</td>\n",
       "      <td> 116</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Chicago Bulls</td>\n",
       "      <td>  95</td>\n",
       "      <td>          Miami Heat</td>\n",
       "      <td> 107</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Brooklyn Nets</td>\n",
       "      <td>  94</td>\n",
       "      <td> Cleveland Cavaliers</td>\n",
       "      <td>  98</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Atlanta Hawks</td>\n",
       "      <td> 109</td>\n",
       "      <td>    Dallas Mavericks</td>\n",
       "      <td> 118</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>   Washington Wizards</td>\n",
       "      <td> 102</td>\n",
       "      <td>     Detroit Pistons</td>\n",
       "      <td> 113</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date Score Type          Visitor Team  VisitorPts  \\\n",
       "0 2013-10-29  Box Score         Orlando Magic          87   \n",
       "1 2013-10-29  Box Score  Los Angeles Clippers         103   \n",
       "2 2013-10-29  Box Score         Chicago Bulls          95   \n",
       "3 2013-10-30  Box Score         Brooklyn Nets          94   \n",
       "4 2013-10-30  Box Score         Atlanta Hawks         109   \n",
       "5 2013-10-30  Box Score    Washington Wizards         102   \n",
       "\n",
       "             Home Team  HomePts  OT? Notes HomeWin HomeLastWin VisitorLastWin  \n",
       "0       Indiana Pacers       97  NaN   NaN    True       False          False  \n",
       "1   Los Angeles Lakers      116  NaN   NaN    True       False          False  \n",
       "2           Miami Heat      107  NaN   NaN    True       False          False  \n",
       "3  Cleveland Cavaliers       98  NaN   NaN    True       False          False  \n",
       "4     Dallas Mavericks      118  NaN   NaN    True       False          False  \n",
       "5      Detroit Pistons      113  NaN   NaN    True       False          False  "
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results[\"HomeLastWin\"] = False\n",
    "results[\"VisitorLastWin\"] = False\n",
    "# This creates two new columns, all set to False\n",
    "results.ix[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Score Type</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>OT?</th>\n",
       "      <th>Notes</th>\n",
       "      <th>HomeWin</th>\n",
       "      <th>HomeLastWin</th>\n",
       "      <th>VisitorLastWin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>2013-11-01</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Milwaukee Bucks</td>\n",
       "      <td> 105</td>\n",
       "      <td>     Boston Celtics</td>\n",
       "      <td>  98</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>2013-11-01</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>             Miami Heat</td>\n",
       "      <td> 100</td>\n",
       "      <td>      Brooklyn Nets</td>\n",
       "      <td> 101</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td>  True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>2013-11-01</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>    Cleveland Cavaliers</td>\n",
       "      <td>  84</td>\n",
       "      <td>  Charlotte Bobcats</td>\n",
       "      <td>  90</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td>  True</td>\n",
       "      <td> False</td>\n",
       "      <td>  True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>2013-11-01</td>\n",
       "      <td> Box Score</td>\n",
       "      <td> Portland Trail Blazers</td>\n",
       "      <td> 113</td>\n",
       "      <td>     Denver Nuggets</td>\n",
       "      <td>  98</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>2013-11-01</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>       Dallas Mavericks</td>\n",
       "      <td> 105</td>\n",
       "      <td>    Houston Rockets</td>\n",
       "      <td> 113</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td>  True</td>\n",
       "      <td>  True</td>\n",
       "      <td>  True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>2013-11-01</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>      San Antonio Spurs</td>\n",
       "      <td>  91</td>\n",
       "      <td> Los Angeles Lakers</td>\n",
       "      <td>  85</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td>  True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Date Score Type            Visitor Team  VisitorPts  \\\n",
       "20 2013-11-01  Box Score         Milwaukee Bucks         105   \n",
       "21 2013-11-01  Box Score              Miami Heat         100   \n",
       "22 2013-11-01  Box Score     Cleveland Cavaliers          84   \n",
       "23 2013-11-01  Box Score  Portland Trail Blazers         113   \n",
       "24 2013-11-01  Box Score        Dallas Mavericks         105   \n",
       "25 2013-11-01  Box Score       San Antonio Spurs          91   \n",
       "\n",
       "             Home Team  HomePts  OT? Notes HomeWin HomeLastWin VisitorLastWin  \n",
       "20      Boston Celtics       98  NaN   NaN   False       False          False  \n",
       "21       Brooklyn Nets      101  NaN   NaN    True       False          False  \n",
       "22   Charlotte Bobcats       90  NaN   NaN    True       False           True  \n",
       "23      Denver Nuggets       98  NaN   NaN   False       False          False  \n",
       "24     Houston Rockets      113  NaN   NaN    True        True           True  \n",
       "25  Los Angeles Lakers       85  NaN   NaN   False       False           True  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Now compute the actual values for these\n",
    "# Did the home and visitor teams win their last game?\n",
    "from collections import defaultdict\n",
    "won_last = defaultdict(int)\n",
    "\n",
    "for index, row in results.iterrows():  # Note that this is not efficient\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    row[\"HomeLastWin\"] = won_last[home_team]\n",
    "    row[\"VisitorLastWin\"] = won_last[visitor_team]\n",
    "    results.ix[index] = row    \n",
    "    # Set current win\n",
    "    won_last[home_team] = row[\"HomeWin\"]\n",
    "    won_last[visitor_team] = not row[\"HomeWin\"]\n",
    "results.ix[20:25]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "clf = DecisionTreeClassifier(random_state=14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using just the last result from the home and visitor teams\n",
      "Accuracy: 57.8%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.cross_validation import cross_val_score\n",
    "\n",
    "# Create a dataset with just the neccessary information\n",
    "X_previouswins = results[[\"HomeLastWin\", \"VisitorLastWin\"]].values\n",
    "clf = DecisionTreeClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_previouswins, y_true, scoring='accuracy')\n",
    "print(\"Using just the last result from the home and visitor teams\")\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# What about win streaks?\n",
    "results[\"HomeWinStreak\"] = 0\n",
    "results[\"VisitorWinStreak\"] = 0\n",
    "# Did the home and visitor teams win their last game?\n",
    "from collections import defaultdict\n",
    "win_streak = defaultdict(int)\n",
    "\n",
    "for index, row in results.iterrows():  # Note that this is not efficient\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    row[\"HomeWinStreak\"] = win_streak[home_team]\n",
    "    row[\"VisitorWinStreak\"] = win_streak[visitor_team]\n",
    "    results.ix[index] = row    \n",
    "    # Set current win\n",
    "    if row[\"HomeWin\"]:\n",
    "        win_streak[home_team] += 1\n",
    "        win_streak[visitor_team] = 0\n",
    "    else:\n",
    "        win_streak[home_team] = 0\n",
    "        win_streak[visitor_team] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using whether the home team is ranked higher\n",
      "Accuracy: 56.1%\n"
     ]
    }
   ],
   "source": [
    "clf = DecisionTreeClassifier(random_state=14)\n",
    "X_winstreak =  results[[\"HomeLastWin\", \"VisitorLastWin\", \"HomeWinStreak\", \"VisitorWinStreak\"]].values\n",
    "scores = cross_val_score(clf, X_winstreak, y_true, scoring='accuracy')\n",
    "print(\"Using whether the home team is ranked higher\")\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Rk</th>\n",
       "      <th>Team</th>\n",
       "      <th>Overall</th>\n",
       "      <th>Home</th>\n",
       "      <th>Road</th>\n",
       "      <th>E</th>\n",
       "      <th>W</th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "      <th>SE</th>\n",
       "      <th>...</th>\n",
       "      <th>Post</th>\n",
       "      <th>≤3</th>\n",
       "      <th>≥10</th>\n",
       "      <th>Oct</th>\n",
       "      <th>Nov</th>\n",
       "      <th>Dec</th>\n",
       "      <th>Jan</th>\n",
       "      <th>Feb</th>\n",
       "      <th>Mar</th>\n",
       "      <th>Apr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0 </th>\n",
       "      <td>  1</td>\n",
       "      <td>             Miami Heat</td>\n",
       "      <td> 66-16</td>\n",
       "      <td>  37-4</td>\n",
       "      <td> 29-12</td>\n",
       "      <td> 41-11</td>\n",
       "      <td>  25-5</td>\n",
       "      <td> 14-4</td>\n",
       "      <td> 12-6</td>\n",
       "      <td> 15-1</td>\n",
       "      <td>...</td>\n",
       "      <td>  30-2</td>\n",
       "      <td>  9-3</td>\n",
       "      <td>  39-8</td>\n",
       "      <td> 1-0</td>\n",
       "      <td> 10-3</td>\n",
       "      <td> 10-5</td>\n",
       "      <td>  8-5</td>\n",
       "      <td> 12-1</td>\n",
       "      <td> 17-1</td>\n",
       "      <td> 8-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1 </th>\n",
       "      <td>  2</td>\n",
       "      <td>  Oklahoma City Thunder</td>\n",
       "      <td> 60-22</td>\n",
       "      <td>  34-7</td>\n",
       "      <td> 26-15</td>\n",
       "      <td>  21-9</td>\n",
       "      <td> 39-13</td>\n",
       "      <td>  7-3</td>\n",
       "      <td>  8-2</td>\n",
       "      <td>  6-4</td>\n",
       "      <td>...</td>\n",
       "      <td>  21-8</td>\n",
       "      <td>  3-6</td>\n",
       "      <td>  44-6</td>\n",
       "      <td> NaN</td>\n",
       "      <td> 13-4</td>\n",
       "      <td> 11-2</td>\n",
       "      <td> 11-5</td>\n",
       "      <td>  7-4</td>\n",
       "      <td> 12-5</td>\n",
       "      <td> 6-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2 </th>\n",
       "      <td>  3</td>\n",
       "      <td>      San Antonio Spurs</td>\n",
       "      <td> 58-24</td>\n",
       "      <td>  35-6</td>\n",
       "      <td> 23-18</td>\n",
       "      <td>  25-5</td>\n",
       "      <td> 33-19</td>\n",
       "      <td>  8-2</td>\n",
       "      <td>  9-1</td>\n",
       "      <td>  8-2</td>\n",
       "      <td>...</td>\n",
       "      <td> 16-12</td>\n",
       "      <td>  9-5</td>\n",
       "      <td> 31-10</td>\n",
       "      <td> 1-0</td>\n",
       "      <td> 12-4</td>\n",
       "      <td> 12-4</td>\n",
       "      <td> 12-3</td>\n",
       "      <td>  8-3</td>\n",
       "      <td> 10-4</td>\n",
       "      <td> 3-6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3 </th>\n",
       "      <td>  4</td>\n",
       "      <td>         Denver Nuggets</td>\n",
       "      <td> 57-25</td>\n",
       "      <td>  38-3</td>\n",
       "      <td> 19-22</td>\n",
       "      <td> 19-11</td>\n",
       "      <td> 38-14</td>\n",
       "      <td>  5-5</td>\n",
       "      <td> 10-0</td>\n",
       "      <td>  4-6</td>\n",
       "      <td>...</td>\n",
       "      <td>  24-4</td>\n",
       "      <td> 11-7</td>\n",
       "      <td>  28-8</td>\n",
       "      <td> 0-1</td>\n",
       "      <td>  8-8</td>\n",
       "      <td>  9-6</td>\n",
       "      <td> 12-3</td>\n",
       "      <td>  8-4</td>\n",
       "      <td> 13-2</td>\n",
       "      <td> 7-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4 </th>\n",
       "      <td>  5</td>\n",
       "      <td>   Los Angeles Clippers</td>\n",
       "      <td> 56-26</td>\n",
       "      <td>  32-9</td>\n",
       "      <td> 24-17</td>\n",
       "      <td>  21-9</td>\n",
       "      <td> 35-17</td>\n",
       "      <td>  7-3</td>\n",
       "      <td>  8-2</td>\n",
       "      <td>  6-4</td>\n",
       "      <td>...</td>\n",
       "      <td>  17-9</td>\n",
       "      <td>  3-5</td>\n",
       "      <td> 38-12</td>\n",
       "      <td> 1-0</td>\n",
       "      <td>  8-6</td>\n",
       "      <td> 16-0</td>\n",
       "      <td>  9-7</td>\n",
       "      <td>  8-5</td>\n",
       "      <td>  7-7</td>\n",
       "      <td> 7-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5 </th>\n",
       "      <td>  6</td>\n",
       "      <td>      Memphis Grizzlies</td>\n",
       "      <td> 56-26</td>\n",
       "      <td>  32-9</td>\n",
       "      <td> 24-17</td>\n",
       "      <td>  22-8</td>\n",
       "      <td> 34-18</td>\n",
       "      <td>  8-2</td>\n",
       "      <td>  8-2</td>\n",
       "      <td>  6-4</td>\n",
       "      <td>...</td>\n",
       "      <td>  23-8</td>\n",
       "      <td>  6-4</td>\n",
       "      <td>  28-9</td>\n",
       "      <td> 0-1</td>\n",
       "      <td> 12-1</td>\n",
       "      <td>  7-7</td>\n",
       "      <td> 10-7</td>\n",
       "      <td>  9-2</td>\n",
       "      <td> 11-6</td>\n",
       "      <td> 7-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6 </th>\n",
       "      <td>  7</td>\n",
       "      <td>        New York Knicks</td>\n",
       "      <td> 54-28</td>\n",
       "      <td> 31-10</td>\n",
       "      <td> 23-18</td>\n",
       "      <td> 37-15</td>\n",
       "      <td> 17-13</td>\n",
       "      <td> 10-6</td>\n",
       "      <td> 12-6</td>\n",
       "      <td> 15-3</td>\n",
       "      <td>...</td>\n",
       "      <td> 22-10</td>\n",
       "      <td>  7-5</td>\n",
       "      <td> 31-12</td>\n",
       "      <td> NaN</td>\n",
       "      <td> 11-4</td>\n",
       "      <td> 10-5</td>\n",
       "      <td>  7-6</td>\n",
       "      <td>  6-5</td>\n",
       "      <td> 12-6</td>\n",
       "      <td> 8-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7 </th>\n",
       "      <td>  8</td>\n",
       "      <td>          Brooklyn Nets</td>\n",
       "      <td> 49-33</td>\n",
       "      <td> 26-15</td>\n",
       "      <td> 23-18</td>\n",
       "      <td> 36-16</td>\n",
       "      <td> 13-17</td>\n",
       "      <td> 11-5</td>\n",
       "      <td> 13-5</td>\n",
       "      <td> 12-6</td>\n",
       "      <td>...</td>\n",
       "      <td> 18-11</td>\n",
       "      <td>  9-4</td>\n",
       "      <td> 23-17</td>\n",
       "      <td> NaN</td>\n",
       "      <td> 11-4</td>\n",
       "      <td> 5-11</td>\n",
       "      <td> 11-4</td>\n",
       "      <td>  7-5</td>\n",
       "      <td>  8-7</td>\n",
       "      <td> 7-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8 </th>\n",
       "      <td>  9</td>\n",
       "      <td>         Indiana Pacers</td>\n",
       "      <td> 49-32</td>\n",
       "      <td> 30-11</td>\n",
       "      <td> 19-21</td>\n",
       "      <td> 31-20</td>\n",
       "      <td> 18-12</td>\n",
       "      <td> 6-11</td>\n",
       "      <td> 13-3</td>\n",
       "      <td> 12-6</td>\n",
       "      <td>...</td>\n",
       "      <td> 17-11</td>\n",
       "      <td>  4-9</td>\n",
       "      <td> 27-14</td>\n",
       "      <td> 1-0</td>\n",
       "      <td>  7-8</td>\n",
       "      <td> 10-5</td>\n",
       "      <td>  9-6</td>\n",
       "      <td>  9-3</td>\n",
       "      <td> 11-5</td>\n",
       "      <td> 2-5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9 </th>\n",
       "      <td> 10</td>\n",
       "      <td>  Golden State Warriors</td>\n",
       "      <td> 47-35</td>\n",
       "      <td> 28-13</td>\n",
       "      <td> 19-22</td>\n",
       "      <td> 19-11</td>\n",
       "      <td> 28-24</td>\n",
       "      <td>  7-3</td>\n",
       "      <td>  5-5</td>\n",
       "      <td>  7-3</td>\n",
       "      <td>...</td>\n",
       "      <td> 17-13</td>\n",
       "      <td>  5-3</td>\n",
       "      <td> 20-18</td>\n",
       "      <td> 1-0</td>\n",
       "      <td>  8-6</td>\n",
       "      <td> 12-4</td>\n",
       "      <td>  8-7</td>\n",
       "      <td>  4-8</td>\n",
       "      <td>  9-7</td>\n",
       "      <td> 5-3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td> 11</td>\n",
       "      <td>          Chicago Bulls</td>\n",
       "      <td> 45-37</td>\n",
       "      <td> 24-17</td>\n",
       "      <td> 21-20</td>\n",
       "      <td> 34-18</td>\n",
       "      <td> 11-19</td>\n",
       "      <td> 13-5</td>\n",
       "      <td>  9-7</td>\n",
       "      <td> 12-6</td>\n",
       "      <td>...</td>\n",
       "      <td> 15-15</td>\n",
       "      <td> 11-7</td>\n",
       "      <td> 16-16</td>\n",
       "      <td> 1-0</td>\n",
       "      <td>  6-7</td>\n",
       "      <td>  9-6</td>\n",
       "      <td> 12-4</td>\n",
       "      <td>  5-8</td>\n",
       "      <td>  7-7</td>\n",
       "      <td> 5-5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td> 12</td>\n",
       "      <td>        Houston Rockets</td>\n",
       "      <td> 45-37</td>\n",
       "      <td> 29-12</td>\n",
       "      <td> 16-25</td>\n",
       "      <td>  21-9</td>\n",
       "      <td> 24-28</td>\n",
       "      <td>  7-3</td>\n",
       "      <td>  7-3</td>\n",
       "      <td>  7-3</td>\n",
       "      <td>...</td>\n",
       "      <td> 16-11</td>\n",
       "      <td>  5-5</td>\n",
       "      <td> 26-13</td>\n",
       "      <td> 1-0</td>\n",
       "      <td>  6-8</td>\n",
       "      <td> 10-6</td>\n",
       "      <td>  8-9</td>\n",
       "      <td>  6-5</td>\n",
       "      <td>  9-5</td>\n",
       "      <td> 5-4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td> 13</td>\n",
       "      <td>     Los Angeles Lakers</td>\n",
       "      <td> 45-37</td>\n",
       "      <td> 29-12</td>\n",
       "      <td> 16-25</td>\n",
       "      <td> 17-13</td>\n",
       "      <td> 28-24</td>\n",
       "      <td>  6-4</td>\n",
       "      <td>  6-4</td>\n",
       "      <td>  5-5</td>\n",
       "      <td>...</td>\n",
       "      <td>  20-8</td>\n",
       "      <td>  8-5</td>\n",
       "      <td> 18-17</td>\n",
       "      <td> 0-2</td>\n",
       "      <td>  8-6</td>\n",
       "      <td>  7-7</td>\n",
       "      <td> 5-11</td>\n",
       "      <td>  9-4</td>\n",
       "      <td>  9-6</td>\n",
       "      <td> 7-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td> 14</td>\n",
       "      <td>          Atlanta Hawks</td>\n",
       "      <td> 44-38</td>\n",
       "      <td> 25-16</td>\n",
       "      <td> 19-22</td>\n",
       "      <td> 29-23</td>\n",
       "      <td> 15-15</td>\n",
       "      <td> 7-11</td>\n",
       "      <td> 11-7</td>\n",
       "      <td> 11-5</td>\n",
       "      <td>...</td>\n",
       "      <td> 15-16</td>\n",
       "      <td>  5-5</td>\n",
       "      <td> 19-20</td>\n",
       "      <td> NaN</td>\n",
       "      <td>  9-5</td>\n",
       "      <td> 10-5</td>\n",
       "      <td>  7-9</td>\n",
       "      <td>  7-4</td>\n",
       "      <td> 8-10</td>\n",
       "      <td> 3-5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td> 15</td>\n",
       "      <td>              Utah Jazz</td>\n",
       "      <td> 43-39</td>\n",
       "      <td> 30-11</td>\n",
       "      <td> 13-28</td>\n",
       "      <td> 17-13</td>\n",
       "      <td> 26-26</td>\n",
       "      <td>  5-5</td>\n",
       "      <td>  5-5</td>\n",
       "      <td>  7-3</td>\n",
       "      <td>...</td>\n",
       "      <td> 13-15</td>\n",
       "      <td>  5-7</td>\n",
       "      <td> 19-21</td>\n",
       "      <td> 1-0</td>\n",
       "      <td>  8-8</td>\n",
       "      <td>  6-9</td>\n",
       "      <td> 10-4</td>\n",
       "      <td>  6-6</td>\n",
       "      <td>  7-9</td>\n",
       "      <td> 5-3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td> 16</td>\n",
       "      <td>         Boston Celtics</td>\n",
       "      <td> 41-40</td>\n",
       "      <td> 27-13</td>\n",
       "      <td> 14-27</td>\n",
       "      <td> 27-24</td>\n",
       "      <td> 14-16</td>\n",
       "      <td>  7-9</td>\n",
       "      <td>  8-9</td>\n",
       "      <td> 12-6</td>\n",
       "      <td>...</td>\n",
       "      <td> 13-16</td>\n",
       "      <td>  8-7</td>\n",
       "      <td> 18-23</td>\n",
       "      <td> 0-1</td>\n",
       "      <td>  9-6</td>\n",
       "      <td>  5-9</td>\n",
       "      <td>  8-7</td>\n",
       "      <td>  8-4</td>\n",
       "      <td>  8-8</td>\n",
       "      <td> 3-5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td> 17</td>\n",
       "      <td>       Dallas Mavericks</td>\n",
       "      <td> 41-41</td>\n",
       "      <td> 24-17</td>\n",
       "      <td> 17-24</td>\n",
       "      <td> 17-13</td>\n",
       "      <td> 24-28</td>\n",
       "      <td>  5-5</td>\n",
       "      <td>  6-4</td>\n",
       "      <td>  6-4</td>\n",
       "      <td>...</td>\n",
       "      <td> 18-12</td>\n",
       "      <td>  5-8</td>\n",
       "      <td> 17-19</td>\n",
       "      <td> 1-1</td>\n",
       "      <td>  6-8</td>\n",
       "      <td> 5-10</td>\n",
       "      <td>  7-8</td>\n",
       "      <td>  6-5</td>\n",
       "      <td> 11-5</td>\n",
       "      <td> 5-4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td> 18</td>\n",
       "      <td>        Milwaukee Bucks</td>\n",
       "      <td> 38-44</td>\n",
       "      <td> 21-20</td>\n",
       "      <td> 17-24</td>\n",
       "      <td> 24-28</td>\n",
       "      <td> 14-16</td>\n",
       "      <td> 11-7</td>\n",
       "      <td>  7-9</td>\n",
       "      <td> 6-12</td>\n",
       "      <td>...</td>\n",
       "      <td> 12-19</td>\n",
       "      <td>  7-5</td>\n",
       "      <td> 13-25</td>\n",
       "      <td> NaN</td>\n",
       "      <td>  7-7</td>\n",
       "      <td>  9-6</td>\n",
       "      <td>  8-7</td>\n",
       "      <td>  4-8</td>\n",
       "      <td>  7-9</td>\n",
       "      <td> 3-7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td> 19</td>\n",
       "      <td>     Philadelphia 76ers</td>\n",
       "      <td> 34-48</td>\n",
       "      <td> 23-18</td>\n",
       "      <td> 11-30</td>\n",
       "      <td> 22-30</td>\n",
       "      <td> 12-18</td>\n",
       "      <td>  7-9</td>\n",
       "      <td> 7-11</td>\n",
       "      <td> 8-10</td>\n",
       "      <td>...</td>\n",
       "      <td> 12-19</td>\n",
       "      <td>  4-5</td>\n",
       "      <td> 13-24</td>\n",
       "      <td> 1-0</td>\n",
       "      <td>  9-6</td>\n",
       "      <td> 4-11</td>\n",
       "      <td>  5-9</td>\n",
       "      <td>  3-8</td>\n",
       "      <td>  8-9</td>\n",
       "      <td> 4-5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td> 20</td>\n",
       "      <td>        Toronto Raptors</td>\n",
       "      <td> 34-48</td>\n",
       "      <td> 21-20</td>\n",
       "      <td> 13-28</td>\n",
       "      <td> 22-30</td>\n",
       "      <td> 12-18</td>\n",
       "      <td> 5-11</td>\n",
       "      <td> 8-10</td>\n",
       "      <td>  9-9</td>\n",
       "      <td>...</td>\n",
       "      <td> 13-16</td>\n",
       "      <td>  8-8</td>\n",
       "      <td> 16-22</td>\n",
       "      <td> 0-1</td>\n",
       "      <td> 4-12</td>\n",
       "      <td>  7-7</td>\n",
       "      <td> 5-10</td>\n",
       "      <td>  7-5</td>\n",
       "      <td> 4-11</td>\n",
       "      <td> 7-2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td> 21</td>\n",
       "      <td> Portland Trail Blazers</td>\n",
       "      <td> 33-49</td>\n",
       "      <td> 22-19</td>\n",
       "      <td> 11-30</td>\n",
       "      <td> 15-15</td>\n",
       "      <td> 18-34</td>\n",
       "      <td>  5-5</td>\n",
       "      <td>  5-5</td>\n",
       "      <td>  5-5</td>\n",
       "      <td>...</td>\n",
       "      <td>  8-21</td>\n",
       "      <td>  9-6</td>\n",
       "      <td> 13-24</td>\n",
       "      <td> 1-0</td>\n",
       "      <td> 5-10</td>\n",
       "      <td>  9-4</td>\n",
       "      <td>  8-8</td>\n",
       "      <td>  3-9</td>\n",
       "      <td>  7-9</td>\n",
       "      <td> 0-9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td> 22</td>\n",
       "      <td> Minnesota Timberwolves</td>\n",
       "      <td> 31-51</td>\n",
       "      <td> 20-21</td>\n",
       "      <td> 11-30</td>\n",
       "      <td> 14-16</td>\n",
       "      <td> 17-35</td>\n",
       "      <td>  4-6</td>\n",
       "      <td>  7-3</td>\n",
       "      <td>  3-7</td>\n",
       "      <td>...</td>\n",
       "      <td> 12-20</td>\n",
       "      <td> 3-10</td>\n",
       "      <td> 15-26</td>\n",
       "      <td> NaN</td>\n",
       "      <td>  7-8</td>\n",
       "      <td>  7-5</td>\n",
       "      <td> 3-12</td>\n",
       "      <td> 3-10</td>\n",
       "      <td> 6-11</td>\n",
       "      <td> 5-5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td> 23</td>\n",
       "      <td>        Detroit Pistons</td>\n",
       "      <td> 29-53</td>\n",
       "      <td> 18-23</td>\n",
       "      <td> 11-30</td>\n",
       "      <td> 25-27</td>\n",
       "      <td>  4-26</td>\n",
       "      <td> 6-12</td>\n",
       "      <td>  8-8</td>\n",
       "      <td> 11-7</td>\n",
       "      <td>...</td>\n",
       "      <td>  8-20</td>\n",
       "      <td> 6-10</td>\n",
       "      <td> 15-29</td>\n",
       "      <td> 0-1</td>\n",
       "      <td> 5-11</td>\n",
       "      <td> 6-10</td>\n",
       "      <td>  6-7</td>\n",
       "      <td>  6-8</td>\n",
       "      <td> 1-13</td>\n",
       "      <td> 5-3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td> 24</td>\n",
       "      <td>     Washington Wizards</td>\n",
       "      <td> 29-53</td>\n",
       "      <td> 22-19</td>\n",
       "      <td>  7-34</td>\n",
       "      <td> 15-37</td>\n",
       "      <td> 14-16</td>\n",
       "      <td> 5-13</td>\n",
       "      <td> 5-13</td>\n",
       "      <td> 5-11</td>\n",
       "      <td>...</td>\n",
       "      <td> 14-17</td>\n",
       "      <td>  6-9</td>\n",
       "      <td> 13-17</td>\n",
       "      <td> 0-1</td>\n",
       "      <td> 1-12</td>\n",
       "      <td> 3-11</td>\n",
       "      <td>  7-9</td>\n",
       "      <td>  7-5</td>\n",
       "      <td>  9-8</td>\n",
       "      <td> 2-7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td> 25</td>\n",
       "      <td>       Sacramento Kings</td>\n",
       "      <td> 28-54</td>\n",
       "      <td> 20-21</td>\n",
       "      <td>  8-33</td>\n",
       "      <td> 14-16</td>\n",
       "      <td> 14-38</td>\n",
       "      <td>  4-6</td>\n",
       "      <td>  4-6</td>\n",
       "      <td>  6-4</td>\n",
       "      <td>...</td>\n",
       "      <td>  9-19</td>\n",
       "      <td>  7-3</td>\n",
       "      <td> 12-31</td>\n",
       "      <td> 0-1</td>\n",
       "      <td> 4-10</td>\n",
       "      <td>  7-8</td>\n",
       "      <td> 6-11</td>\n",
       "      <td>  3-9</td>\n",
       "      <td>  7-8</td>\n",
       "      <td> 1-7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td> 26</td>\n",
       "      <td>    New Orleans Hornets</td>\n",
       "      <td> 27-55</td>\n",
       "      <td> 16-25</td>\n",
       "      <td> 11-30</td>\n",
       "      <td> 12-18</td>\n",
       "      <td> 15-37</td>\n",
       "      <td>  3-7</td>\n",
       "      <td>  5-5</td>\n",
       "      <td>  4-6</td>\n",
       "      <td>...</td>\n",
       "      <td>  8-21</td>\n",
       "      <td>  7-5</td>\n",
       "      <td> 10-27</td>\n",
       "      <td> 0-1</td>\n",
       "      <td>  4-9</td>\n",
       "      <td> 3-13</td>\n",
       "      <td>  8-8</td>\n",
       "      <td>  5-8</td>\n",
       "      <td>  6-9</td>\n",
       "      <td> 1-7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td> 27</td>\n",
       "      <td>           Phoenix Suns</td>\n",
       "      <td> 25-57</td>\n",
       "      <td> 17-24</td>\n",
       "      <td>  8-33</td>\n",
       "      <td>  8-22</td>\n",
       "      <td> 17-35</td>\n",
       "      <td>  1-9</td>\n",
       "      <td>  4-6</td>\n",
       "      <td>  3-7</td>\n",
       "      <td>...</td>\n",
       "      <td>  8-21</td>\n",
       "      <td>  6-8</td>\n",
       "      <td> 10-31</td>\n",
       "      <td> 0-1</td>\n",
       "      <td>  7-9</td>\n",
       "      <td> 4-11</td>\n",
       "      <td>  5-9</td>\n",
       "      <td>  4-9</td>\n",
       "      <td> 3-12</td>\n",
       "      <td> 2-6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td> 28</td>\n",
       "      <td>    Cleveland Cavaliers</td>\n",
       "      <td> 24-58</td>\n",
       "      <td> 14-27</td>\n",
       "      <td> 10-31</td>\n",
       "      <td> 18-34</td>\n",
       "      <td>  6-24</td>\n",
       "      <td> 5-13</td>\n",
       "      <td> 3-13</td>\n",
       "      <td> 10-8</td>\n",
       "      <td>...</td>\n",
       "      <td>  8-21</td>\n",
       "      <td> 6-11</td>\n",
       "      <td>  7-27</td>\n",
       "      <td> 1-0</td>\n",
       "      <td> 3-12</td>\n",
       "      <td> 3-13</td>\n",
       "      <td>  6-8</td>\n",
       "      <td>  7-5</td>\n",
       "      <td> 2-12</td>\n",
       "      <td> 2-8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td> 29</td>\n",
       "      <td>      Charlotte Bobcats</td>\n",
       "      <td> 21-61</td>\n",
       "      <td> 15-26</td>\n",
       "      <td>  6-35</td>\n",
       "      <td> 18-34</td>\n",
       "      <td>  3-27</td>\n",
       "      <td> 6-12</td>\n",
       "      <td> 6-12</td>\n",
       "      <td> 6-10</td>\n",
       "      <td>...</td>\n",
       "      <td>  9-21</td>\n",
       "      <td>  6-6</td>\n",
       "      <td>  6-37</td>\n",
       "      <td> NaN</td>\n",
       "      <td>  7-8</td>\n",
       "      <td> 1-15</td>\n",
       "      <td> 3-11</td>\n",
       "      <td> 2-10</td>\n",
       "      <td> 4-12</td>\n",
       "      <td> 4-5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td> 30</td>\n",
       "      <td>          Orlando Magic</td>\n",
       "      <td> 20-62</td>\n",
       "      <td> 12-29</td>\n",
       "      <td>  8-33</td>\n",
       "      <td> 10-42</td>\n",
       "      <td> 10-20</td>\n",
       "      <td> 2-16</td>\n",
       "      <td> 5-13</td>\n",
       "      <td> 3-13</td>\n",
       "      <td>...</td>\n",
       "      <td>  5-25</td>\n",
       "      <td>  2-9</td>\n",
       "      <td>  8-30</td>\n",
       "      <td> NaN</td>\n",
       "      <td> 5-10</td>\n",
       "      <td>  7-9</td>\n",
       "      <td> 2-12</td>\n",
       "      <td> 2-11</td>\n",
       "      <td> 3-13</td>\n",
       "      <td> 1-7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>30 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Rk                    Team Overall   Home   Road      E      W     A  \\\n",
       "0    1              Miami Heat   66-16   37-4  29-12  41-11   25-5  14-4   \n",
       "1    2   Oklahoma City Thunder   60-22   34-7  26-15   21-9  39-13   7-3   \n",
       "2    3       San Antonio Spurs   58-24   35-6  23-18   25-5  33-19   8-2   \n",
       "3    4          Denver Nuggets   57-25   38-3  19-22  19-11  38-14   5-5   \n",
       "4    5    Los Angeles Clippers   56-26   32-9  24-17   21-9  35-17   7-3   \n",
       "5    6       Memphis Grizzlies   56-26   32-9  24-17   22-8  34-18   8-2   \n",
       "6    7         New York Knicks   54-28  31-10  23-18  37-15  17-13  10-6   \n",
       "7    8           Brooklyn Nets   49-33  26-15  23-18  36-16  13-17  11-5   \n",
       "8    9          Indiana Pacers   49-32  30-11  19-21  31-20  18-12  6-11   \n",
       "9   10   Golden State Warriors   47-35  28-13  19-22  19-11  28-24   7-3   \n",
       "10  11           Chicago Bulls   45-37  24-17  21-20  34-18  11-19  13-5   \n",
       "11  12         Houston Rockets   45-37  29-12  16-25   21-9  24-28   7-3   \n",
       "12  13      Los Angeles Lakers   45-37  29-12  16-25  17-13  28-24   6-4   \n",
       "13  14           Atlanta Hawks   44-38  25-16  19-22  29-23  15-15  7-11   \n",
       "14  15               Utah Jazz   43-39  30-11  13-28  17-13  26-26   5-5   \n",
       "15  16          Boston Celtics   41-40  27-13  14-27  27-24  14-16   7-9   \n",
       "16  17        Dallas Mavericks   41-41  24-17  17-24  17-13  24-28   5-5   \n",
       "17  18         Milwaukee Bucks   38-44  21-20  17-24  24-28  14-16  11-7   \n",
       "18  19      Philadelphia 76ers   34-48  23-18  11-30  22-30  12-18   7-9   \n",
       "19  20         Toronto Raptors   34-48  21-20  13-28  22-30  12-18  5-11   \n",
       "20  21  Portland Trail Blazers   33-49  22-19  11-30  15-15  18-34   5-5   \n",
       "21  22  Minnesota Timberwolves   31-51  20-21  11-30  14-16  17-35   4-6   \n",
       "22  23         Detroit Pistons   29-53  18-23  11-30  25-27   4-26  6-12   \n",
       "23  24      Washington Wizards   29-53  22-19   7-34  15-37  14-16  5-13   \n",
       "24  25        Sacramento Kings   28-54  20-21   8-33  14-16  14-38   4-6   \n",
       "25  26     New Orleans Hornets   27-55  16-25  11-30  12-18  15-37   3-7   \n",
       "26  27            Phoenix Suns   25-57  17-24   8-33   8-22  17-35   1-9   \n",
       "27  28     Cleveland Cavaliers   24-58  14-27  10-31  18-34   6-24  5-13   \n",
       "28  29       Charlotte Bobcats   21-61  15-26   6-35  18-34   3-27  6-12   \n",
       "29  30           Orlando Magic   20-62  12-29   8-33  10-42  10-20  2-16   \n",
       "\n",
       "       C    SE  ...    Post    ≤3    ≥10  Oct   Nov   Dec   Jan   Feb   Mar  \\\n",
       "0   12-6  15-1  ...    30-2   9-3   39-8  1-0  10-3  10-5   8-5  12-1  17-1   \n",
       "1    8-2   6-4  ...    21-8   3-6   44-6  NaN  13-4  11-2  11-5   7-4  12-5   \n",
       "2    9-1   8-2  ...   16-12   9-5  31-10  1-0  12-4  12-4  12-3   8-3  10-4   \n",
       "3   10-0   4-6  ...    24-4  11-7   28-8  0-1   8-8   9-6  12-3   8-4  13-2   \n",
       "4    8-2   6-4  ...    17-9   3-5  38-12  1-0   8-6  16-0   9-7   8-5   7-7   \n",
       "5    8-2   6-4  ...    23-8   6-4   28-9  0-1  12-1   7-7  10-7   9-2  11-6   \n",
       "6   12-6  15-3  ...   22-10   7-5  31-12  NaN  11-4  10-5   7-6   6-5  12-6   \n",
       "7   13-5  12-6  ...   18-11   9-4  23-17  NaN  11-4  5-11  11-4   7-5   8-7   \n",
       "8   13-3  12-6  ...   17-11   4-9  27-14  1-0   7-8  10-5   9-6   9-3  11-5   \n",
       "9    5-5   7-3  ...   17-13   5-3  20-18  1-0   8-6  12-4   8-7   4-8   9-7   \n",
       "10   9-7  12-6  ...   15-15  11-7  16-16  1-0   6-7   9-6  12-4   5-8   7-7   \n",
       "11   7-3   7-3  ...   16-11   5-5  26-13  1-0   6-8  10-6   8-9   6-5   9-5   \n",
       "12   6-4   5-5  ...    20-8   8-5  18-17  0-2   8-6   7-7  5-11   9-4   9-6   \n",
       "13  11-7  11-5  ...   15-16   5-5  19-20  NaN   9-5  10-5   7-9   7-4  8-10   \n",
       "14   5-5   7-3  ...   13-15   5-7  19-21  1-0   8-8   6-9  10-4   6-6   7-9   \n",
       "15   8-9  12-6  ...   13-16   8-7  18-23  0-1   9-6   5-9   8-7   8-4   8-8   \n",
       "16   6-4   6-4  ...   18-12   5-8  17-19  1-1   6-8  5-10   7-8   6-5  11-5   \n",
       "17   7-9  6-12  ...   12-19   7-5  13-25  NaN   7-7   9-6   8-7   4-8   7-9   \n",
       "18  7-11  8-10  ...   12-19   4-5  13-24  1-0   9-6  4-11   5-9   3-8   8-9   \n",
       "19  8-10   9-9  ...   13-16   8-8  16-22  0-1  4-12   7-7  5-10   7-5  4-11   \n",
       "20   5-5   5-5  ...    8-21   9-6  13-24  1-0  5-10   9-4   8-8   3-9   7-9   \n",
       "21   7-3   3-7  ...   12-20  3-10  15-26  NaN   7-8   7-5  3-12  3-10  6-11   \n",
       "22   8-8  11-7  ...    8-20  6-10  15-29  0-1  5-11  6-10   6-7   6-8  1-13   \n",
       "23  5-13  5-11  ...   14-17   6-9  13-17  0-1  1-12  3-11   7-9   7-5   9-8   \n",
       "24   4-6   6-4  ...    9-19   7-3  12-31  0-1  4-10   7-8  6-11   3-9   7-8   \n",
       "25   5-5   4-6  ...    8-21   7-5  10-27  0-1   4-9  3-13   8-8   5-8   6-9   \n",
       "26   4-6   3-7  ...    8-21   6-8  10-31  0-1   7-9  4-11   5-9   4-9  3-12   \n",
       "27  3-13  10-8  ...    8-21  6-11   7-27  1-0  3-12  3-13   6-8   7-5  2-12   \n",
       "28  6-12  6-10  ...    9-21   6-6   6-37  NaN   7-8  1-15  3-11  2-10  4-12   \n",
       "29  5-13  3-13  ...    5-25   2-9   8-30  NaN  5-10   7-9  2-12  2-11  3-13   \n",
       "\n",
       "    Apr  \n",
       "0   8-1  \n",
       "1   6-2  \n",
       "2   3-6  \n",
       "3   7-1  \n",
       "4   7-1  \n",
       "5   7-2  \n",
       "6   8-2  \n",
       "7   7-2  \n",
       "8   2-5  \n",
       "9   5-3  \n",
       "10  5-5  \n",
       "11  5-4  \n",
       "12  7-1  \n",
       "13  3-5  \n",
       "14  5-3  \n",
       "15  3-5  \n",
       "16  5-4  \n",
       "17  3-7  \n",
       "18  4-5  \n",
       "19  7-2  \n",
       "20  0-9  \n",
       "21  5-5  \n",
       "22  5-3  \n",
       "23  2-7  \n",
       "24  1-7  \n",
       "25  1-7  \n",
       "26  2-6  \n",
       "27  2-8  \n",
       "28  4-5  \n",
       "29  1-7  \n",
       "\n",
       "[30 rows x 24 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Let's try see which team is better on the ladder. Using the previous year's ladder\n",
    "ladder_filename = os.path.join(data_folder, \"leagues_NBA_2013_standings_expanded-standings.csv\")\n",
    "ladder = pd.read_csv(ladder_filename, skiprows=[0,1])\n",
    "ladder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Score Type</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>OT?</th>\n",
       "      <th>Notes</th>\n",
       "      <th>HomeWin</th>\n",
       "      <th>HomeLastWin</th>\n",
       "      <th>VisitorLastWin</th>\n",
       "      <th>HomeWinStreak</th>\n",
       "      <th>VisitorWinStreak</th>\n",
       "      <th>HomeTeamRanksHigher</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Orlando Magic</td>\n",
       "      <td>  87</td>\n",
       "      <td>      Indiana Pacers</td>\n",
       "      <td>  97</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td> Los Angeles Clippers</td>\n",
       "      <td> 103</td>\n",
       "      <td>  Los Angeles Lakers</td>\n",
       "      <td> 116</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Chicago Bulls</td>\n",
       "      <td>  95</td>\n",
       "      <td>          Miami Heat</td>\n",
       "      <td> 107</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Brooklyn Nets</td>\n",
       "      <td>  94</td>\n",
       "      <td> Cleveland Cavaliers</td>\n",
       "      <td>  98</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Atlanta Hawks</td>\n",
       "      <td> 109</td>\n",
       "      <td>    Dallas Mavericks</td>\n",
       "      <td> 118</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date Score Type          Visitor Team  VisitorPts  \\\n",
       "0 2013-10-29  Box Score         Orlando Magic          87   \n",
       "1 2013-10-29  Box Score  Los Angeles Clippers         103   \n",
       "2 2013-10-29  Box Score         Chicago Bulls          95   \n",
       "3 2013-10-30  Box Score         Brooklyn Nets          94   \n",
       "4 2013-10-30  Box Score         Atlanta Hawks         109   \n",
       "\n",
       "             Home Team  HomePts  OT? Notes HomeWin HomeLastWin VisitorLastWin  \\\n",
       "0       Indiana Pacers       97  NaN   NaN    True       False          False   \n",
       "1   Los Angeles Lakers      116  NaN   NaN    True       False          False   \n",
       "2           Miami Heat      107  NaN   NaN    True       False          False   \n",
       "3  Cleveland Cavaliers       98  NaN   NaN    True       False          False   \n",
       "4     Dallas Mavericks      118  NaN   NaN    True       False          False   \n",
       "\n",
       "   HomeWinStreak  VisitorWinStreak  HomeTeamRanksHigher  \n",
       "0              0                 0                    0  \n",
       "1              0                 0                    1  \n",
       "2              0                 0                    0  \n",
       "3              0                 0                    1  \n",
       "4              0                 0                    1  "
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We can create a new feature -- HomeTeamRanksHigher\\\n",
    "results[\"HomeTeamRanksHigher\"] = 0\n",
    "for index, row in results.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    if home_team == \"New Orleans Pelicans\":\n",
    "        home_team = \"New Orleans Hornets\"\n",
    "    elif visitor_team == \"New Orleans Pelicans\":\n",
    "        visitor_team = \"New Orleans Hornets\"\n",
    "    home_rank = ladder[ladder[\"Team\"] == home_team][\"Rk\"].values[0]\n",
    "    visitor_rank = ladder[ladder[\"Team\"] == visitor_team][\"Rk\"].values[0]\n",
    "    row[\"HomeTeamRanksHigher\"] = int(home_rank > visitor_rank)\n",
    "    results.ix[index] = row\n",
    "results[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using whether the home team is ranked higher\n",
      "Accuracy: 60.3%\n"
     ]
    }
   ],
   "source": [
    "X_homehigher =  results[[\"HomeLastWin\", \"VisitorLastWin\", \"HomeTeamRanksHigher\"]].values\n",
    "clf = DecisionTreeClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_homehigher, y_true, scoring='accuracy')\n",
    "print(\"Using whether the home team is ranked higher\")\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 60.6%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.grid_search import GridSearchCV\n",
    "\n",
    "parameter_space = {\n",
    "                   \"max_depth\": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],\n",
    "                   }\n",
    "clf = DecisionTreeClassifier(random_state=14)\n",
    "grid = GridSearchCV(clf, parameter_space)\n",
    "grid.fit(X_homehigher, y_true)\n",
    "print(\"Accuracy: {0:.1f}%\".format(grid.best_score_ * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Score Type</th>\n",
       "      <th>Visitor Team</th>\n",
       "      <th>VisitorPts</th>\n",
       "      <th>Home Team</th>\n",
       "      <th>HomePts</th>\n",
       "      <th>OT?</th>\n",
       "      <th>Notes</th>\n",
       "      <th>HomeWin</th>\n",
       "      <th>HomeLastWin</th>\n",
       "      <th>VisitorLastWin</th>\n",
       "      <th>HomeWinStreak</th>\n",
       "      <th>VisitorWinStreak</th>\n",
       "      <th>HomeTeamRanksHigher</th>\n",
       "      <th>HomeTeamWonLast</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Orlando Magic</td>\n",
       "      <td>  87</td>\n",
       "      <td>      Indiana Pacers</td>\n",
       "      <td>  97</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td> Los Angeles Clippers</td>\n",
       "      <td> 103</td>\n",
       "      <td>  Los Angeles Lakers</td>\n",
       "      <td> 116</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 1</td>\n",
       "      <td> 0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2013-10-29</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Chicago Bulls</td>\n",
       "      <td>  95</td>\n",
       "      <td>          Miami Heat</td>\n",
       "      <td> 107</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Brooklyn Nets</td>\n",
       "      <td>  94</td>\n",
       "      <td> Cleveland Cavaliers</td>\n",
       "      <td>  98</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 1</td>\n",
       "      <td> 0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>        Atlanta Hawks</td>\n",
       "      <td> 109</td>\n",
       "      <td>    Dallas Mavericks</td>\n",
       "      <td> 118</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 1</td>\n",
       "      <td> 0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2013-10-30</td>\n",
       "      <td> Box Score</td>\n",
       "      <td>   Washington Wizards</td>\n",
       "      <td> 102</td>\n",
       "      <td>     Detroit Pistons</td>\n",
       "      <td> 113</td>\n",
       "      <td> NaN</td>\n",
       "      <td> NaN</td>\n",
       "      <td> True</td>\n",
       "      <td> False</td>\n",
       "      <td> False</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "      <td> 0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date Score Type          Visitor Team  VisitorPts  \\\n",
       "0 2013-10-29  Box Score         Orlando Magic          87   \n",
       "1 2013-10-29  Box Score  Los Angeles Clippers         103   \n",
       "2 2013-10-29  Box Score         Chicago Bulls          95   \n",
       "3 2013-10-30  Box Score         Brooklyn Nets          94   \n",
       "4 2013-10-30  Box Score         Atlanta Hawks         109   \n",
       "5 2013-10-30  Box Score    Washington Wizards         102   \n",
       "\n",
       "             Home Team  HomePts  OT? Notes HomeWin HomeLastWin VisitorLastWin  \\\n",
       "0       Indiana Pacers       97  NaN   NaN    True       False          False   \n",
       "1   Los Angeles Lakers      116  NaN   NaN    True       False          False   \n",
       "2           Miami Heat      107  NaN   NaN    True       False          False   \n",
       "3  Cleveland Cavaliers       98  NaN   NaN    True       False          False   \n",
       "4     Dallas Mavericks      118  NaN   NaN    True       False          False   \n",
       "5      Detroit Pistons      113  NaN   NaN    True       False          False   \n",
       "\n",
       "   HomeWinStreak  VisitorWinStreak  HomeTeamRanksHigher  HomeTeamWonLast  \n",
       "0              0                 0                    0                0  \n",
       "1              0                 0                    1                0  \n",
       "2              0                 0                    0                0  \n",
       "3              0                 0                    1                0  \n",
       "4              0                 0                    1                0  \n",
       "5              0                 0                    0                0  "
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Who won the last match? We ignore home/visitor for this bit\n",
    "last_match_winner = defaultdict(int)\n",
    "results[\"HomeTeamWonLast\"] = 0\n",
    "\n",
    "for index, row in results.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    teams = tuple(sorted([home_team, visitor_team]))  # Sort for a consistent ordering\n",
    "    # Set in the row, who won the last encounter\n",
    "    row[\"HomeTeamWonLast\"] = 1 if last_match_winner[teams] == row[\"Home Team\"] else 0\n",
    "    results.ix[index] = row\n",
    "    # Who won this one?\n",
    "    winner = row[\"Home Team\"] if row[\"HomeWin\"] else row[\"Visitor Team\"]\n",
    "    last_match_winner[teams] = winner\n",
    "results.ix[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using whether the home team is ranked higher\n",
      "Accuracy: 60.6%\n"
     ]
    }
   ],
   "source": [
    "X_home_higher =  results[[\"HomeTeamRanksHigher\", \"HomeTeamWonLast\"]].values\n",
    "clf = DecisionTreeClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_home_higher, y_true, scoring='accuracy')\n",
    "print(\"Using whether the home team is ranked higher\")\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 60.0%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import LabelEncoder, OneHotEncoder\n",
    "encoding = LabelEncoder()\n",
    "encoding.fit(results[\"Home Team\"].values)\n",
    "home_teams = encoding.transform(results[\"Home Team\"].values)\n",
    "visitor_teams = encoding.transform(results[\"Visitor Team\"].values)\n",
    "X_teams = np.vstack([home_teams, visitor_teams]).T\n",
    "\n",
    "onehot = OneHotEncoder()\n",
    "X_teams = onehot.fit_transform(X_teams).todense()\n",
    "\n",
    "clf = DecisionTreeClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_teams, y_true, scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using full team labels is ranked higher\n",
      "Accuracy: 60.6%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "clf = RandomForestClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_teams, y_true, scoring='accuracy')\n",
    "print(\"Using full team labels is ranked higher\")\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1230, 62)\n"
     ]
    }
   ],
   "source": [
    "X_all = np.hstack([X_home_higher, X_teams])\n",
    "print(X_all.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using whether the home team is ranked higher\n",
      "Accuracy: 61.1%\n"
     ]
    }
   ],
   "source": [
    "clf = RandomForestClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_all, y_true, scoring='accuracy')\n",
    "print(\"Using whether the home team is ranked higher\")\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 64.2%\n",
      "RandomForestClassifier(bootstrap=True, compute_importances=None,\n",
      "            criterion='entropy', max_depth=None, max_features=2,\n",
      "            max_leaf_nodes=None, min_density=None, min_samples_leaf=6,\n",
      "            min_samples_split=2, n_estimators=100, n_jobs=1,\n",
      "            oob_score=False, random_state=14, verbose=0)\n"
     ]
    }
   ],
   "source": [
    "#n_estimators=10, criterion='gini', max_depth=None, \n",
    "#min_samples_split=2, min_samples_leaf=1,\n",
    "#max_features='auto',\n",
    "#max_leaf_nodes=None, bootstrap=True,\n",
    "#oob_score=False, n_jobs=1,\n",
    "#random_state=None, verbose=0, min_density=None, compute_importances=None\n",
    "parameter_space = {\n",
    "                   \"max_features\": [2, 10, 'auto'],\n",
    "                   \"n_estimators\": [100,],\n",
    "                   \"criterion\": [\"gini\", \"entropy\"],\n",
    "                   \"min_samples_leaf\": [2, 4, 6],\n",
    "                   }\n",
    "clf = RandomForestClassifier(random_state=14)\n",
    "grid = GridSearchCV(clf, parameter_space)\n",
    "grid.fit(X_all, y_true)\n",
    "print(\"Accuracy: {0:.1f}%\".format(grid.best_score_ * 100))\n",
    "print(grid.best_estimator_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
